//
//  LeiJiXiaoShouEDetailViewController.m
//  FactorySale
//
//  Created by 李 on 2020/1/13.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "LeiJiXiaoShouEDetailViewController.h"
#import "LeiJiXiaoShouEDetailListTopCell.h"
#import "LeiJiXiaoShouEDetailModel.h"
#import "LeiJiXiaoShouEDetailContentCell.h"
@interface LeiJiXiaoShouEDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
//列表
@property (nonatomic,strong) UITableView *listTableView;
@property (nonatomic,strong) NSMutableArray *listArray;
@property (nonatomic,strong) NSMutableArray *relsListArray;
@property (nonatomic,assign)NSInteger currentClickSection;
@property (weak, nonatomic) IBOutlet UIView *noDataBgView;

@end

static NSString *const LeiJiXiaoShouEDetailListTopCellID = @"LeiJiXiaoShouEDetailListTopCell";

@implementation LeiJiXiaoShouEDetailViewController
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"累计销售额";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = Main_BackGroundColor;
    [self addBackTableView];
    [self requestSalesVolume];
    
}
-(void)requestSalesVolume
{
    NSDictionary *param = @{
        @"id":NOTNIL(self.id),
        @"begTime":NOTNIL(self.begTime),
        @"endTime":NOTNIL(self.endTime)
    };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:salesVolumeInfo param:param success:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        self.listArray = [LeiJiXiaoShouEDetailModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        [self.listTableView reloadData];
        
    } other:^(id dic) {
        [self.view bringSubviewToFront:self.noDataBgView];
        self.noDataBgView.hidden = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:NO];
}
-(void)addBackTableView
{
    [self.listTableView registerNib:[UINib nibWithNibName:NSStringFromClass([LeiJiXiaoShouEDetailListTopCell class]) bundle:nil] forCellReuseIdentifier:LeiJiXiaoShouEDetailListTopCellID];
    
    [self.view addSubview:self.listTableView];
    
    
}
- (UITableView *)listTableView
{
    if (!_listTableView) {
        _listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,kNaviHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kNaviHeight-TabbarSafeBottomMargin) style:UITableViewStylePlain];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _listTableView.estimatedSectionHeaderHeight = 0;
        _listTableView.estimatedSectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor =  Main_BackGroundColor;
    }
    return _listTableView;
}
#pragma mark UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 70;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 34.5;
    
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LeiJiXiaoShouEDetailModel *model = [self.listArray safeObjectAtIndex:section];
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = Main_BackGroundColor;
    headerView.frame = CGRectMake(15, 0, KWIDTH-30, 70);
    
    UIView *backheaderView = [[UIView alloc] init];
    backheaderView.backgroundColor = [UIColor whiteColor];
    backheaderView.frame = CGRectMake(15, 0, KWIDTH-30, 70);
    [headerView addSubview:backheaderView];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-30, 10)];
    lineView.backgroundColor =Main_BackGroundColor;
    [backheaderView addSubview:lineView];
    
    UIImageView *greenImage = [[UIImageView alloc] init];
    greenImage.frame = CGRectMake(0, 10, KWIDTH-30, 5);
    greenImage.image = imgname(@"圆角矩形 782 拷贝");
    [backheaderView addSubview:greenImage];
    
    UILabel *timeLab = [[UILabel alloc]initWithFrame:CGRectMake(15, greenImage.bottom+10, 200, 10)];
    timeLab.font = KFontPingFangSCRegular(10);
    timeLab.textColor =[UIColor colorWithHexString:@"#999999"];
    timeLab.text = model.create_time;
    [backheaderView addSubview:timeLab];
    
    //厂家名称
    UILabel *nameLab = [[UILabel alloc]init];
    nameLab.font = KFontPingFangSCRegular(13);
    nameLab.textColor =[UIColor colorWithHexString:@"#333333"];
    nameLab.textAlignment = NSTextAlignmentLeft;
    nameLab.text = model.name;
    [backheaderView addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLab.mas_bottom).mas_offset(11);
        make.left.equalTo(backheaderView.mas_left).mas_offset(15);
        make.height.mas_equalTo(13);
        make.width.mas_greaterThanOrEqualTo(52);
        
    }];
    
    //订单金额
    UILabel *moneyLab = [[UILabel alloc]init];
    moneyLab.font = KFontPingFangSCRegular(11);
    moneyLab.textColor =[UIColor colorWithHexString:@"#999999"];
    moneyLab.textAlignment = NSTextAlignmentRight;
    moneyLab.text = [NSString stringWithFormat:@"订单金额：%@元",model.total_amount];
    [backheaderView addSubview:moneyLab];
    [moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backheaderView.mas_top).mas_offset(46.5);
        make.right.equalTo(backheaderView.mas_right).mas_offset(-15);
        make.height.mas_equalTo(11);
        make.width.mas_greaterThanOrEqualTo(52);
    }];
    
    UIView *line2 = [[UIView alloc] init];
    line2.backgroundColor = RGBCOLOR(229, 229, 229);
    [backheaderView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLab.mas_bottom).mas_offset(12);
        make.left.equalTo(backheaderView.mas_left).mas_offset(0);
        make.right.equalTo(backheaderView.mas_right).mas_offset(0);
        make.height.mas_equalTo(0.5);
        make.bottom.equalTo(backheaderView.mas_bottom).mas_offset(0);
    }];
    return headerView;
    
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    LeiJiXiaoShouEDetailModel *model = [self.listArray safeObjectAtIndex:section];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, 34.5)];
    footerView.backgroundColor = Main_BackGroundColor;
    
    UIView *footerView2 = [[UIView alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, 34.5)];
    footerView2.backgroundColor = [UIColor whiteColor];
    [footerView addSubview:footerView2];
    
    UIButton *zhanKaiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    zhanKaiBtn.frame = CGRectMake((ScreenW-30-44)/2, 0, 44, 44);
    [zhanKaiBtn addTarget:self action:@selector(zhanKaiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    zhanKaiBtn.tag = section+100;
    [footerView2 addSubview:zhanKaiBtn];
    if (model.isOpen) {
        [zhanKaiBtn setImage:[UIImage imageNamed:@"向下"] forState:UIControlStateNormal];
        
    }else
    {
        [zhanKaiBtn setImage:[UIImage imageNamed:@"向上"] forState:UIControlStateNormal];
        
    }
    
    return footerView;
}
-(void)zhanKaiBtnClick:(UIButton *)btn
{
    self.currentClickSection = btn.tag-100;
    
    LeiJiXiaoShouEDetailModel *heZuoModel = [self.listArray safeObjectAtIndex:self.currentClickSection];
    heZuoModel.isOpen = !heZuoModel.isOpen;
    if (heZuoModel.isOpen) {
        [btn setImage:[UIImage imageNamed:@"向下"] forState:UIControlStateNormal];
        
    }else
    {
        [btn setImage:[UIImage imageNamed:@"向上"] forState:UIControlStateNormal];
        
    }
    for (int i=0; i<self.listArray.count; i++) {
        if (i == self.currentClickSection) {
            
        }else
        {
            LeiJiXiaoShouEDetailModel *model = [self.listArray safeObjectAtIndex:i];
            model.isOpen = NO;
        }
    }
    [self.listTableView reloadData];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.listArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (self.currentClickSection ==section) {
        LeiJiXiaoShouEDetailModel *model = [self.listArray safeObjectAtIndex:section];
        if (model.isOpen) {
            self.relsListArray = [LeiJiXiaoShouEDetailRelsListModel mj_objectArrayWithKeyValuesArray:model.rels];
            return self.relsListArray.count +1;
        }else
        {
            return 0;
        }
        
    }else
    {
        return 0;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row ==0) {
        return 0;
    } else {
        return 80;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        /**
         这个类已经废弃了
         */
        LeiJiXiaoShouEDetailListTopCell *cell = [tableView dequeueReusableCellWithIdentifier:LeiJiXiaoShouEDetailListTopCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row>0) {
        
        LeiJiXiaoShouEDetailModel *model = [LeiJiXiaoShouEDetailModel mj_objectWithKeyValues:[self.listArray safeObjectAtIndex:indexPath.section]];
        self.relsListArray = [LeiJiXiaoShouEDetailRelsListModel mj_objectArrayWithKeyValuesArray:model.rels];
        
        LeiJiXiaoShouEDetailRelsListModel *xingHaoModel = [LeiJiXiaoShouEDetailRelsListModel mj_objectWithKeyValues:[self.relsListArray safeObjectAtIndex:indexPath.row-1]];
        
        LeiJiXiaoShouEDetailContentCell *cell = [LeiJiXiaoShouEDetailContentCell cellWithTableView:tableView];
        cell.model = xingHaoModel;
        return cell;
        
    }
    NSString *cellIdentifier = @"iden3";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    return cell;
    
}

@end
